package com.mc.dao.Impl;

import com.mc.bean.Match;
import com.mc.dao.MatchDao;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangcf
 * @version 1.0
 * @Description 赛事类数据访问接口实现类
 * @date 2023/2/13 13:16
 */
public class MatchDaoImpl extends BaseDao implements MatchDao {

    public Match newMatch() throws SQLException {
        Match match = new Match(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getTimestamp(4), rs.getTimestamp(5), rs.getInt(6), rs.getInt(7));
        return match;
    }

    @Override
    public List<Match> findAll() {
        List<Match> matchList = new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match`");
            getRs();
            while (rs.next()) {
                Match match = newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }

        return matchList;
    }

    @Override
    public Match findByMid(int mid) {
        Match match = null;
        try {
            regist();
            connection();
            getPst("select * from `match` where mid=?");
            pstmt.setInt(1,mid);
            getRs();
            while (rs.next()){
                match=newMatch();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return match;
    }

    @Override
    public List<Match> findByMSid(int msid) {
        List<Match> matchList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match` where msid=?");
            pstmt.setInt(1,msid);
            getRs();
            while (rs.next()){
                Match match=newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchList;
    }

    @Override
    public List<Match> findByMTid(int mtid) {
        List<Match> matchList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match` where mtid=?");
            pstmt.setInt(1,mtid);
            getRs();
            while (rs.next()){
                Match match=newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchList;
    }

    @Override
    public List<Match> find(String mname) {
        List<Match> matchList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match` where mname like ?");
            pstmt.setString(1,"%"+mname+"%");
            getRs();
            while (rs.next()){
                Match match=newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchList;
    }

    @Override
    public List<Match> findStartTime() {
        List<Match> matchList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match` where UNIX_TIMESTAMP(starttime)>UNIX_TIMESTAMP(NOW())");
            getRs();
            while (rs.next()){
                Match match=newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchList;
    }

    @Override
    public List<Match> findEndTime() {
        List<Match> matchList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from `match` where UNIX_TIMESTAMP(endtime)>UNIX_TIMESTAMP(NOW())");
            getRs();
            while (rs.next()){
                Match match=newMatch();
                matchList.add(match);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchList;
    }

    @Override
    public int add(Match match) {
        int i=0;
        try {
            regist();
            connection();
            getPst("insert into `match` (mname,overview,starttime,endtime,msid,mtid) values (?,?,?,?,?,?)");
            pstmt.setString(1,match.getMname());
            pstmt.setString(2,match.getOverview());
            pstmt.setTimestamp(3,match.getStarttime());
            pstmt.setTimestamp(4,match.getEndtime());
            pstmt.setInt(5,match.getMsid());
            pstmt.setInt(6,match.getMtid());
            i= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int delete(int mid) {
        int i=0;
        try {
            regist();
            connection();
            getPst("delete from `match` where mid=?");
            pstmt.setInt(1,mid);
            i= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int update(Match match) {
        int i=0;
        try {
            regist();
            connection();
            getPst("update `match` set mname=?,overview=?,starttime=?,endtime=?,msid=?,mtid=? where mid=?");
            pstmt.setString(1,match.getMname());
            pstmt.setString(2,match.getOverview());
            pstmt.setTimestamp(3,match.getStarttime());
            pstmt.setTimestamp(4,match.getEndtime());
            pstmt.setInt(5,match.getMsid());
            pstmt.setInt(6,match.getMtid());
            pstmt.setInt(7,match.getMid());
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
}
